#include <iostream>

using std::cout;
using std::endl;

class Stack
{
public:
    Stack(int size = 10)
    : _top(-1) //栈顶指针初始值为-1
    , _size(size)
    , _base(new int[_size]())
    {}

    ~Stack()
    {   
        if(_base) 
        {
            delete [] _base;
            _base = nullptr;
        }
    }

    bool empty() const
    {   return _top == -1;   }

    bool full() const
    {   return _top == _size - 1; }

    void push(int num) {
        if(full()) {
            cout << "stack is full, cannot push any data" << endl;
        } else {
            _base[++_top] = num;
        }
    }

    void pop() {
        if(empty()) {
            cout << "stack is empty, no more data" << endl;
        } else {
            --_top;
        }
    }

    //该函数使用前要先做判断，有元素时才能正常调用
    int top() const
    {   return _base[_top]; }

private:
    //用指针的写法更灵活
    int _top;
    int _size;
    int * _base;
};

int main()
{
    Stack s;//s中默认情况下最多能存放10个元素
    cout << "s是否为空？" << s.empty() << endl;
    s.push(1);
    cout << "s是否为空？" << s.empty() << endl;

    for(int i = 2; i < 12; ++i) {
        s.push(i);
    }
    cout << "s是否已满？" << s.full() << endl;

    while(!s.empty()) {
        cout << s.top() << endl;//先打印栈顶元素
        s.pop();//栈顶元素出栈
    }
    cout << "s是否已满？" << s.full() << endl;
    cout << "s是否为空？" << s.empty() << endl;

    return 0;
}

